package com.fujimana.rockincat.restree.phase;

import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import com.fujimana.rockincat.restree.service.Response;

public class PrepareResponsePhase implements Phase {

	@Override
	public void execute(Map<String, Object> context) {
		HttpServletResponse response = (HttpServletResponse) context
				.get(HttpServletResponse.class.getName());

		String contentType = (String) context.get("contentType");
		Locale locale = (Locale) context.get("locale");
		String charset = (String) context.get("charset");

		Set<String> keys = Response.getResponse().getHeaderKeys();
		for (String key : keys) {
			response.setHeader(key, Response.getResponse().getHeader(key));
		}

		response.setCharacterEncoding(charset);
		response.setLocale(locale);
		response.setContentType(contentType);
		response.setStatus(Response.getResponse().getStatus());
	}

	@Override
	public String getId() {
		return PrepareResponsePhase.class.getSimpleName();
	}

	@Override
	public boolean isChain() {
		return true;
	}

}
